/**
 * 
 */
package org.xlab.baseframework.listener;

import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.xlab.baseframework.domain.SystemCode;
import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.exception.SystemCodeNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.service.api.ISystemCodeService;
import org.xlab.baseframework.service.spi.ISystemInitializationListener;
import org.xlab.baseframework.system.PlatformDeclare;
import org.xlab.baseframework.system.SystemRuntimeEnvironment;

/**
 * @author MaKuBEX
 * @since 0.1.16
 */
@Component
public class SystemCodeLoader implements ISystemInitializationListener {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 8917786736050965463L;
	private static final Logger log = Logger.getLogger(SystemCodeLoader.class);
	@Autowired(required = true)
	@Qualifier("bpf_systemCodeService")
	private ISystemCodeService systemCodeService;
	@Autowired(required = true)
	@Qualifier("bpf_systemCodeServiceMessageSource")
	private MessageSource messageSource;

	/**
	 * @return the messageSource
	 */
	public MessageSource getMessageSource() {
		return messageSource;
	}

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#getSequence()
	 */
	public int getSequence() {
		return PlatformDeclare.SEQUENCE_OF_SYSTEM_CODE_LOADER;
	}

	/**
	 * @return the systemCodeService
	 */
	public ISystemCodeService getSystemCodeService() {
		return systemCodeService;
	}

	/**
	 * @see org.xlab.baseframework.service.spi.ISystemInitializationListener#onSystemStarted()
	 */
	public void onSystemStarted() throws SystemRuntimeException {
		String message = null;
		try {
			message = this.messageSource.getMessage(
					"info.listener.runtime.env.loadingcodes", null,
					"Loading all system codes...", Locale.getDefault());
			log.debug(message);
			List<SystemCode> allCodes = this.systemCodeService
					.findAllByDeletedInd(EnumDeletedIndentifying.N);
			for (SystemCode code : allCodes) {
				SystemRuntimeEnvironment.setCode(code);
			}
		} catch (SystemCodeNotFoundException e) {
			message = this.messageSource.getMessage(
					"info.listener.runtime.env.emptycodes", null,
					"There is not any system code be found.",
					Locale.getDefault());
			log.warn(message);
		}
	}

	/**
	 * @param messageSource
	 *            the messageSource to set
	 */
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	/**
	 * @param systemCodeService
	 *            the systemCodeService to set
	 */
	public void setSystemCodeService(ISystemCodeService systemCodeService) {
		this.systemCodeService = systemCodeService;
	}

}
